//
//  ViewController.swift
//  SurfEasy
//
//  Created by 薛永伟 on 2019/9/12.
//  Copyright © 2019 XueYongWei. All rights reserved.
//

import UIKit

class RootViewController: UIViewController {

    lazy var windowsHolder: UIView = {
        let view = UIView()
        return view
    }()
    
    lazy var websControllVC: MNWebsController = {
        let vc = MNWebsController()
        addChild(vc)
        vc.didMove(toParent: self)
        vc.windowsPreviewBtn.addTarget(self, action: #selector(onWindowsPreviewClick(sender:)), for: .touchUpInside)
        return vc
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.addSubview(windowsHolder)
        windowsHolder.snp.makeConstraints { (make) in
            make.bottom.left.right.equalToSuperview()
            make.height.equalTo(0.scaleLength)
        }
        
        view.addSubview(websControllVC.view)
        websControllVC.view.snp.makeConstraints { (make) in
            make.top.left.right.equalToSuperview()
            make.bottom.equalTo(windowsHolder.snp.top).offset(0)
        }
        
        let vc = Browser.windowsManager.controllers[0]
        addChild(vc)
        vc.didMove(toParent: self)
        websControllVC.webHolder.addSubview(vc.view)
        websControllVC.webviewController = vc
        vc.view.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
       
//        vc.webview.load(URLRequest.init(url: url))
    }


}

//MARK: - --------------类--------------

//MARK: - --------------响应事件--------------
extension RootViewController{
    
    @objc func onWindowsPreviewClick(sender:UIButton){
        
        sender.isSelected = !sender.isSelected
        
        
        let height = sender.isSelected ? 0 : 128
        windowsHolder.snp.updateConstraints { (make) in
            make.height.equalTo(height)
        }
        
        UIView.animate(withDuration: 0.25) {
            self.view.layoutIfNeeded()
        }
        
        if let webtool = websControllVC.webviewController?.webviewToolBar,let windowsTool = websControllVC.webviewController?.windowsToolBar {
            
            let form = sender.isSelected ? webtool : windowsTool
            
            let to = sender.isSelected ? windowsTool : webtool
            
            UIView.transition(from: form, to: to, duration: 0.25, options: .transitionFlipFromTop) { (finished) in
                
            }
        }
       
    }
}

//MARK: - --------------数据请求--------------

//MARK: - --------------辅助方法--------------

//MARK: - --------------驱动UI--------------
